using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Infrastructure.Repositories
{
    public class PrivacySettingRepository : IPrivacySettingRepository
    {
        private readonly SheLifeDbContext _context;

        public PrivacySettingRepository(SheLifeDbContext context)
        {
            _context = context;
        }

        public async Task<PrivacySetting> GetByIdAsync(int id)
        {
            return await _context.PrivacySettings.FindAsync(id);
        }

        public async Task<PrivacySetting> GetByUserIdAsync(int userId)
        {
            return await _context.PrivacySettings
                .FirstOrDefaultAsync(p => p.UserId == userId);
        }

        public async Task<PrivacySetting> AddAsync(PrivacySetting setting)
        {
            setting.CreatedAt = DateTime.UtcNow;
            await _context.PrivacySettings.AddAsync(setting);
            await _context.SaveChangesAsync();
            return setting;
        }

        public async Task UpdateAsync(PrivacySetting setting)
        {
            setting.UpdatedAt = DateTime.UtcNow;
            _context.Entry(setting).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }

        public async Task DeleteAsync(int id)
        {
            var setting = await GetByIdAsync(id);
            if (setting != null)
            {
                _context.PrivacySettings.Remove(setting);
                await _context.SaveChangesAsync();
            }
        }

        public async Task<bool> ExistsAsync(int id)
        {
            return await _context.PrivacySettings.AnyAsync(p => p.Id == id);
        }

        public async Task<bool> ExistsByUserIdAsync(int userId)
        {
            return await _context.PrivacySettings.AnyAsync(p => p.UserId == userId);
        }

        public async Task<bool> IsDataSharedAsync(int userId, string dataType)
        {
            var setting = await GetByUserIdAsync(userId);
            if (setting == null)
                return false;

            return dataType switch
            {
                "HealthData" => setting.ShareHealthData,
                "LifestyleData" => setting.ShareLifestyleData,
                "SkincareData" => setting.ShareSkincareData,
                "ProfileData" => setting.ShareProfileData,
                _ => false
            };
        }

        public async Task UpdateDataSharingAsync(int userId, string dataType, bool isShared)
        {
            var setting = await GetByUserIdAsync(userId);
            if (setting == null)
                return;

            switch (dataType)
            {
                case "HealthData":
                    setting.ShareHealthData = isShared;
                    break;
                case "LifestyleData":
                    setting.ShareLifestyleData = isShared;
                    break;
                case "SkincareData":
                    setting.ShareSkincareData = isShared;
                    break;
                case "ProfileData":
                    setting.ShareProfileData = isShared;
                    break;
            }

            setting.UpdatedAt = DateTime.UtcNow;
            await _context.SaveChangesAsync();
        }

        public async Task<bool> IsFeatureEnabledAsync(int userId, string feature)
        {
            var setting = await GetByUserIdAsync(userId);
            if (setting == null)
                return false;

            return feature switch
            {
                "AiAnalysis" => setting.EnableAiAnalysis,
                "DataSync" => setting.EnableDataSync,
                "PersonalizedRecommendations" => setting.EnablePersonalizedRecommendations,
                "Notifications" => setting.EnableNotifications,
                _ => false
            };
        }

        public async Task UpdateFeatureSettingAsync(int userId, string feature, bool isEnabled)
        {
            var setting = await GetByUserIdAsync(userId);
            if (setting == null)
                return;

            switch (feature)
            {
                case "AiAnalysis":
                    setting.EnableAiAnalysis = isEnabled;
                    break;
                case "DataSync":
                    setting.EnableDataSync = isEnabled;
                    break;
                case "PersonalizedRecommendations":
                    setting.EnablePersonalizedRecommendations = isEnabled;
                    break;
                case "Notifications":
                    setting.EnableNotifications = isEnabled;
                    break;
            }

            setting.UpdatedAt = DateTime.UtcNow;
            await _context.SaveChangesAsync();
        }

        public async Task<Dictionary<string, bool>> GetAllPrivacySettingsAsync(int userId)
        {
            var setting = await GetByUserIdAsync(userId);
            if (setting == null)
                return new Dictionary<string, bool>();

            return new Dictionary<string, bool>
            {
                { "ShareHealthData", setting.ShareHealthData },
                { "ShareLifestyleData", setting.ShareLifestyleData },
                { "ShareSkincareData", setting.ShareSkincareData },
                { "ShareProfileData", setting.ShareProfileData },
                { "EnableAiAnalysis", setting.EnableAiAnalysis },
                { "EnableDataSync", setting.EnableDataSync },
                { "EnablePersonalizedRecommendations", setting.EnablePersonalizedRecommendations },
                { "EnableNotifications", setting.EnableNotifications }
            };
        }
    }
}